$(document).ready(function() {
    var args = getArgs();
    var url = args["RelayState"];
    var host = 'https://gw.wmcloud.com';

    $("#btnCode").attr("src", host + "/usermaster/captcha.json?" + new Date());

   $("#btnWeiChat").bind("click", function() {

        $.ajax({
            url: host + '/usermaster/logout.json',
            type: 'GET',
            dataType: 'json',
            timeout: 5000,
            success: function(data) {

        $.ajax({
            url: host + '/usermaster/wechat/login.json',
            type: 'GET',
            dataType: 'json',
            timeout: 5000,
            success: function(data) {
                if (url) {
                    window.location.assign(data.content.replace('#wechat_redirect', encodeURIComponent(encodeURIComponent(url))));
                } else {
                    window.location.assign(data.content);
                }
            }
        });
                   

            }
        });

    });


     $(".weibo_btn").bind("click", function() {

        $.ajax({
            url: host + '/usermaster/logout.json',
            type: 'GET',
            dataType: 'json',
            timeout: 5000,
            success: function(data) {
             
        $.ajax({
            url: host + '/usermaster/weibo/login.json',
            type: 'GET',
            dataType: 'json',
            timeout: 5000,
            success: function(data) {
                if (url) {
                    window.location.assign(data.content.replace('#weibo_redirect', encodeURIComponent(encodeURIComponent(url))));
                } else {
                    window.location.assign(data.content);
                }
            }
        });

            }
        });

    });

    $("#btnCode").bind("click", function() {
        $('#code').val('');
        $(this).attr("src", host + "/usermaster/captcha.json?" + new Date());
    });

    function failToLogin() {
        $(".login_form").css("display", "block");
        $("#btnWeiChat").addClass('show');
        $(".loginIpt").css("display", "block");
        $(".error-message").css("display", "block");
        $(".success-message").css("display", "none");
        $(".valid-message").css("display", "none");
    }

    function tryToLogin() {
        if (tryToLogin.tries++ >= tryToLogin.MaxTries) {
            return failToLogin();
        }
        $.ajax({
            url: host + '/cloud/identity.jsonp',
            type: 'GET',
            dataType: 'jsonp',
            timeout: 5000,
            success: function(data) {
                if (!data || data.anonymous) {
                    console.log("fail");
                    return failToLogin();
                }
                window.location.assign(decodeURIComponent(url || '/cloud-portal/') + "?" + new Date().getTime());
                $(".login_form").css("display", "none");
                $("#btnWeiChat").css("display", "none");
                $(".loginIpt").css("display", "none");

                $(".error-message").css("display", "none");
                $(".valid-message").css("display", "none");
                $(".success-message").css("display", "block");
            },
            error: tryToLogin
        });
    }
    tryToLogin.tries = 0;
    tryToLogin.MaxTries = 3;

    tryToLogin();
});
